<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class UserShareImage extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'user_share_image';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = 'delete_dt';

    // 追加属性
    protected $append = [
        'share_status_text',
        'poster_list',
        'qrcode'
    ];
    

    
    public function getShareStatusList()
    {
        return ['0' => '无效', '1' => '有效'];
    }


    public function getShareStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['share_status']) ? $data['share_status'] : '');
        $list = $this->getShareStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    public function getPosterListAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['poster_images']) ? $data['poster_images'] : '');
        if (!$value) return [];
        $list = explode(',', $value);
        foreach ($list as &$v) {
            $v = config('domain').$v;
        }
        return $list;
    }

    public function setPosterImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['poster_images']) ? $data['poster_images'] : '');
        if (is_array($value)) $value = implode(',', $value);
        return $value;
    }

    public function getQrcodeAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['qrcode']) ? $data['qrcode'] : '');
        return config('domain').$value;
    }
}
